LinuxCommandLibrary

]

Terminate conditional expressions or lists

TLDR

View documentation for the [ keyword

$ tldr [
copy

SYNOPSIS

[ expression ]
(The ] is the mandatory closing argument to the [ test command.)

DESCRIPTION

The ] is not a standalone Linux command but an essential syntactic element in POSIX shells like Bash, Zsh, and Dash. It serves as the required closing argument for the [ (test) builtin command, which evaluates conditional expressions.

The test command syntax is strictly [ expression ], where [ is the opening delimiter treated as the command name, expression is the condition (e.g., file existence, string comparisons, numeric tests), and ] must be the final argument. Omitting ] causes a syntax error like unexpected token near ';'. Typing ] alone in a shell yields command not found, as it lacks executable code.

This construct is foundational for shell scripting conditionals: if [ -f file.txt ]; then echo 'Exists'; fi. It supports unary operators (-f for file, -d for directory), binary (-eq, -lt), and string tests (!=, =). Modern Bash also offers [[...]] for extended features like pattern matching, but [ ] remains portable.

Internally, shells parse [ specially, skipping its execution as argv[0] and validating ] as argv[n-1]. This dates to early Unix, ensuring reliable boolean logic without external binaries.

CAVEATS

Not executable alone; must follow [ expression. Shell-specific parsing; portable only in POSIX mode. Avoid spaces before ] to prevent errors.

COMMON USAGE EXAMPLES

[ -z "$var" ] tests empty string.
[ "$a" = "$b" ] string equality.
[ $num -gt 10 ] numeric greater than.

ERROR HANDLING

Quote variables to avoid word-splitting: [ "$file" = "*.txt" ]. Use [[ ]] in Bash for unquoted globs.

HISTORY

Introduced in Unix Version 7 (1979) as part of the test utility. The [ ] bracket notation added in PWB/1 (1977) for readability over test alone. Standardized in POSIX.1-1988; Bash enhanced with [[ ]] in 1993.

SEE ALSO

test(1), [(1), [[(1), true(1), false(1)

Copied to clipboard